<?php


namespace App\Admin\Controllers\config\forms;


use App\Common\statement\CommonStatement;
use App\Models\config\Config;
use App\Models\config\ConfigCate;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class Create extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '新增配置';


    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        $params = $request->post();
//            dd($params);
        Config::create($params);

        admin_toastr('信息修改成功');

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {



        $this->select('type', '数据类型')->options(CommonStatement::config_directors)->default('text');

        $arr = ConfigCate::select('module','name')->get()->toArray();

        $temp_key = array_column($arr,'module');  //键值
        $array = array_combine($temp_key,array_column($arr,'name')) ;

        $this->select('module', '模块名')->options($array)->default(array_keys($array)[0])
             ->rules('required');
        $this->text('title','配置名')->rules('required');
        $this->text('name','key')
            ->creationRules('required|unique:configs')
            ->updateRules('required|unique:configs,name,{{name}}');
        $this->number('sort',__('排序'))->default(1);
        $this->text('value','value');
        $this->text('rule','验证标识');
        $this->text('help','提示信息');
        $this->select('field_type', '数据处理')->options(CommonStatement::field_type)->default(0);


//        $this->divider();
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        return $this->data;
    }
}
